//
//  ViewController.swift
//  TwoColumnList
//
//  Created by 李煜 on 2024/9/4.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
  @IBOutlet weak var categoryTableView: UITableView!
  @IBOutlet weak var contentTableView: UITableView!
  
  var categories: [String] = ["Category 1", "Category 2", "Category 3"]
  var contents: [String] = []
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    categoryTableView.delegate = self
    categoryTableView.dataSource = self
    contentTableView.delegate = self
    contentTableView.dataSource = self
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == categoryTableView {
      return categories.count
    } else {
      return contents.count
    }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.font = .systemFont(ofSize: 12)
    if tableView == categoryTableView {
      cell.textLabel?.text = categories[indexPath.row]
    } else {
      cell.textLabel?.text = contents[indexPath.row]
    }
    return cell
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == categoryTableView {
      let selectedCategory = categories[indexPath.row]
      updateContentList(for: selectedCategory)
    }
  }
  
  func updateContentList(for category: String) {
    // Mock data for demo purposes
    contents = ["Content 1 for \(category)", "Content 2 for \(category)"]
    contentTableView.reloadData()
  }
}


